package com.luoye.lj.util.date;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 15-11-2
 * Time: 上午10:35
 * Title:        时间处理函数库
 * Description:  时间间隔的计算，时间的加减
 */
public class TimeUtil {
    public static final String INTERVAL_YEAR="year";
    public static final String INTERVAL_QUARTER="quarter";
    public static final String INTERVAL_MONTH="month";
    public static final String INTERVAL_WEEK="week";
    public static final String INTERVAL_DAY="day";
    public static final String INTERVAL_HOUR="hour";
    public static final String INTERVAL_MINUTE="minute";
    public static final String INTERVAL_SECOND="second";


    /**
     * 按指定日期单位计算两个日期间的间隔
     *
     * @param timeInterval(year,quarter,month,week,day,hour,minute,second)
     * @param date1
     * @param date2
     * @return
     */
    public static long DateDiff(String timeInterval, Date date1, Date date2) {
        if (timeInterval.equals("year")) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date1);
            int time = calendar.get(Calendar.YEAR);
            calendar.setTime(date2);
            return time - calendar.get(Calendar.YEAR);
        }

        if (timeInterval.equals("quarter")) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date1);
            int time = calendar.get(Calendar.YEAR) * 4;
            calendar.setTime(date2);
            time -= calendar.get(Calendar.YEAR) * 4;
            calendar.setTime(date1);
            time += calendar.get(Calendar.MONTH) / 4;
            calendar.setTime(date2);
            return time - calendar.get(Calendar.MONTH) / 4;
        }

        if (timeInterval.equals("month")) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date1);
            int time = calendar.get(Calendar.YEAR) * 12;
            calendar.setTime(date2);
            time -= calendar.get(Calendar.YEAR) * 12;
            calendar.setTime(date1);
            time += calendar.get(Calendar.MONTH);
            calendar.setTime(date2);
            return time - calendar.get(Calendar.MONTH);
        }

        if (timeInterval.equals("week")) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date1);
            int time = calendar.get(Calendar.YEAR) * 52;
            calendar.setTime(date2);
            time -= calendar.get(Calendar.YEAR) * 52;
            calendar.setTime(date1);
            time += calendar.get(Calendar.WEEK_OF_YEAR);
            calendar.setTime(date2);
            return time - calendar.get(Calendar.WEEK_OF_YEAR);
        }

        if (timeInterval.equals("day")) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date1);
            int time1 =calendar.get(Calendar.YEAR) * 365;
            time1 +=calendar.get(Calendar.DAY_OF_YEAR);
            calendar.setTime(date2);
            int time2 = calendar.get(Calendar.YEAR) * 365;
            time2 +=calendar.get(Calendar.DAY_OF_YEAR);
            return time1 - time2;
        }

        if (timeInterval.equals("hour")) {
            long time = date1.getTime() / 1000 / 60 / 60;
            return time - date2.getTime() / 1000 / 60 / 60;
        }

        if (timeInterval.equals("minute")) {
            long time = date1.getTime() / 1000 / 60;
            return time - date2.getTime() / 1000 / 60;
        }

        if (timeInterval.equals("second")) {
            long time = date1.getTime() / 1000;
            return time - date2.getTime() / 1000;
        }

        return date1.getTime() - date2.getTime();
    }

    /**
     * 获取当前月的第一天
     * @return Date
     */
    public static Date FirstDayofMonth(Date t) {
       if(t==null){
           t= new Date();
       }
        t.setDate(1);
        t.setHours(0);
        t.setMinutes(0);
        t.setSeconds(0);
        return t;
    }

    /**
     * 获取当前月的最后一天
     * @return Date
     */
    public static Date LastDayofMonth(Date t) {
        if(t==null){
            t= new Date();
        }
        Date FirstDayofNextMonth = addDate(FirstDayofMonth(t), Calendar.MONTH, 1);
        return addDate(FirstDayofNextMonth, Calendar.DATE, -1);
    }

   /******
    * 获取两个时间之间的间隔，分别用天、小时、分、秒表示
    * *****/
    public static Map DateDiffirent(Date startDate, Date endDate){
        long start=startDate.getTime();
        long end=endDate.getTime();
        Map result = new HashMap();
        long day = (end-start)/(1000*60*60*24);
        result.put("day",day);
        long hour = (end-start)/(1000*60*60)-day*24;
        result.put("hour",hour);
        long minute = (end-start)/(1000*60)-day*24*60-hour*60;
        result.put("minute",minute);
        long second = (end-start)/(1000)-day*24*60*60-hour*60*60-minute*60;
        result.put("second",second);

        return result;
    }
    /*******
     * 给时间加上指定的时间间隔
     * ****/
    public static Date addDate(Date date , int timeInterval, int number){
        Calendar cal= Calendar.getInstance();
        cal.setTime(date);
        cal.add(timeInterval,number);
        return cal.getTime();
    }
}
